import Taro from '@tarojs/taro'
import app from '../../../main.js'
let myJs = {
    getData: function() {
        this.state = {
            doctor_id: '',
            cityRang: [[], []],
            case_detail: {
                patient_name: '',
                gender: '',
                birthday: '',
                phone: '',
                province: '',
                city: '',
                occupation: ''
            },
            pdata: {},
            cities: []
        };
        return this.state;
    },

    createCase() {
        console.log(this.state.case_detail);
        var me = this;
        if(!this.state.case_detail.patient_name) {
            app.toastMessage('请填写姓名');
            return;
        }

        if(!this.state.case_detail.gender) {
            app.toastMessage('请选择性别');
            return;
        }

        if(!this.state.case_detail.birthday) {
            app.toastMessage('请选择出生日期');
            return;
        }

        if(!this.state.case_detail.city) {
            app.toastMessage('请选择居住城市');
            return;
        }

        me.state.case_detail.doctor_id = this.state.doctor_id? this.state.doctor_id: app.current.id;
        me.state.case_detail.type = this.$router.params.type;
        if (this.$router.params.project_id) {
            me.state.case_detail.project_id = this.$router.params.project_id;
        }

        app.post("api/case_details/create2", me.state.case_detail, function(res) {
            var id = res.data.result;
            me.redirectTo("../editCase/editCase?id="+id);
        });
    },
    allCities(province_id) {
        var me = this;
        app.get("api/cities/all", {province_id: province_id}, function(res) {
            var data = res.data.result.data;
            me.state.cityRang[1] = [''];
            for(var i = 0; i < data.length; i++) {
                me.state.cityRang[1].push(data[i].name);
            }
            me.setState({
                cityRang: me.state.cityRang
            });
        });
    },
    allProvinces() {
        var me = this;
        app.get("api/provinces/all", {}, function(res) {
            var data = res.data.result.data;
            me.state.cityRang[0] = [''];
            me.state.pdata = {};
            for(var i = 0; i < data.length; i++) {
                me.state.cityRang[0].push(data[i].name);
                me.state.pdata[data[i].name] = data[i].id;
            }
            me.setState({
                cityRang: me.state.cityRang,
                pdata: me.state.pdata
            });

            if(me.state.cityRang[0].length > 0) {
                me.allCities(me.state.pdata[me.state.cityRang[0][0]]);
            }
        });
    },
    componentWillMount () {
        this.init();
    },
    navigateTo(url) {
        app.navigateTo(url);
    },
    handleDetailChange(name, event) {
        var names = name.split('.');
        var n = names[0];
        var temp = this.state;
        for(var i = 0; i < names.length - 1; i++) {
            temp = temp[names[i]];
        }

        if(names.length > 1) {
            temp[names[i]] = event.detail.value;
            this.setState({n: temp});
        } else {
            this.setState({n: event.detail.value});
        }
    },
    handleChange(name, v) {
        var names = name.split('.');
        var n = names[0];
        var temp = this.state;
        for(var i = 0; i < names.length - 1; i++) {
            temp = temp[names[i]];
        }
        
        if(names.length > 1) {
            temp[names[i]] = v;
            this.setState({n: temp});
        } else {
            this.setState({n: v});
        }
    },
    redirectTo(url) {
        app.redirectTo(url);
    },
    componentDidMount () {},
    componentWillUnmount () {},
    componentDidHide () {}
}
export default myJs;
